<template>
  <div class="vip-pay-container">

    <!-- 操作 -->
    <el-row class="vip-pay-row">
      <el-col :span="8" style="text-align:left;padding-left:15px" class="vip-pay-row-col">
        <el-button type="primary" size="small" style="padding:8px 30px;" @click="disVipPanel = true; isAdd = true; rest()">新 增</el-button>
      </el-col>
      <el-col :span="6">
        <span style="padding:0 10px">会员总开关</span>
        <el-switch v-model="switchData.is_open" active-color="#13ce66" @change="putVipSwitch"></el-switch>
      </el-col>
      <el-col :span="10">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:240px">
          <el-button slot="suffix" type="text" size="small" class="search-button" @click="search">
            <i class="el-icon-search"></i>
          </el-button>
        </el-input>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="pagVipList" style="width:100%" stripe>
        <el-table-column prop="name" label="会员等级名称" align="center"></el-table-column>
        <el-table-column prop="min_score" label="等级积分" align="center"></el-table-column>
        <el-table-column prop="discount_ration" label="优惠比例(%)" align="center"></el-table-column>
        <el-table-column prop="voucher_count" label="每月赠送优惠券数量" align="center"></el-table-column>
        <el-table-column prop="score_times" label="会员获取积分倍数" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope" width="80px">
            <el-switch v-model="scope.row.status" active-color="#13ce66" @change="putVipStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="shanchu"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>


    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disVipPanel" width="30%" :title="isAdd ? '新增' : '修改'">
      <vip-panel :vipData="vipData" :isAdd="isAdd" @success="disVipPanel = false; getPayVipList()"/>
    </el-dialog>

  </div>
</template>

<script>
import { vipSwitch, putVipSwitch, getUpPayVips, putUnPayVip, delUpPayVip } from '@/api/vip'
import vipPanel from './components/vipPanel'

export default {
  name: 'vipPay',
  components: {
    vipPanel,
  },
  data() {
    return {
      switchData: {},

      page: 1,
      count: 1,
      searchName: '',

      pagVipList: [],

      disVipPanel: false,
      isAdd: true,

      disVipConfig: false,

      vipData: {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        min_score: '',
        discount_ratio: '',
        voucher_type_id: '',
        score_times: '',
        status: true
      }

    }
  },
  mounted() {
    this.vipSwitch();
    this.getPayVipList();
  },
  methods: {
    /**
     * 编辑
     */
    eidt(data) {
      this.isAdd = false;
      this.vipData = data;
      this.disVipPanel = true;
    },
    /**
     * 修改状态
     */
    putVipStatus(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id,
        status: data.status ? '1' : '0'
      };
      putUnPayVip(params).then(res => {
        if(res.status === 200){
          this.$message.success('修改成功！');
          this.getPayVipList();
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取积分会员列表
     */
    getPayVipList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        searchName: this.searchName,
        page: this.page,
        limit: 10
      }
      getUpPayVips(params).then(response => {
        if(response.status === 200){
          this.pagVipList = response.data;
          this.pagVipList.forEach(item => item.status = item.status === '1' ? true : false);
          this.count = parseInt(response.count);
        }else if(response.status === 204 ){
          this.pagVipList = [];
          this.count = 1;
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 获取会员总开关
     */
    vipSwitch() {
      const params = { key: this.$store.state.app.activeApp.saa_key, english_name: 'Vip_integral' };
      vipSwitch(params).then(response => {
        if(response.status === 200){
          this.switchData = response.data;
          this.switchData.is_open = this.switchData.is_open === '1' ? true : false;
        }else if(response.status === 204){
          this.switchData = {};
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 修改会员总开关
     */
    putVipSwitch() {
      if(this.switchData.is_open){
        this.$confirm('开启积分会员，付费会员将自动关闭，确定开启吗?','提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const params = { key: this.$store.state.app.activeApp.saa_key, english_name: 'Vip_integral', is_open: this.switchData.is_open ? '1' : '0', id: this.switchData.id };
          putVipSwitch(params).then(response => {
            if(response.status === 200){
              this.$message.success('修改成功！');
            }else{
              this.$message.error(response.message);
            }
          })
        }).catch(() => {
          this.switchData.is_open = false
        })
      }else{
        const params = { key: this.$store.state.app.activeApp.saa_key, english_name: 'Vip_payment', is_open: this.switchData.is_open ? '1' : '0', id: this.switchData.id };
        putVipSwitch(params).then(response => {
          if(response.status === 200){
            this.$message.success('修改成功！');
          }else{
            this.$message.error(response.message);
          }
        })
      }
      
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getPayVipList();
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: 1,
        limit: 10,
        searchName: this.searchName
      };

      getUpPayVips(params).then(response => {
        if(response.status === 200){
          this.pagVipList = response.data;
          this.pagVipList.forEach(item => item.status = item.status === '1' ? true : false);
          this.count = parseInt(response.count);
        }else if(response.status === 204 ){
          this.pagVipList = [];
          this.count = 1;
        }else{
          this.$message.error(response.message);
        }
      })
    },
    /**
     * 删除会员
     */
    del(data) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: data.id
      }
      delUpPayVip(params).then(response => {
        if(response.status === 200){
          this.getPayVipList();
          this.$message.success('删除成功！');
        }else{
          this.$message.error(response.message);
        }
      })
    },
    rest() {
      this.vipData = {
          key: this.$store.state.app.activeApp.saa_key,
          name: '',
          min_score: '',
          discount_ratio: '',
          voucher_type_id: '',
          score_times: '',
          status: true
        }
    }
  },
}
</script>

<style lang="scss" scoped>
.vip-pay-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.vip-pay-row{
  margin: 15px 0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
  }
}
</style>